<?php
// +----------------------------------------------------------------------
// | EtrolPHP [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Author: Author: HCZ <huangchengze@etrol.com> 
// +----------------------------------------------------------------------

namespace app\warning\controller\api;
use app\common\controller\Api;

/**
 * 历史预警
 * @package app\warning\api
 * @author HCZ<huangchengze@etrol.com>
 */
class History extends Api{
	
	/**
	 * 历史预警
	 */
	public function getHistoryList(){
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 巡检报告列表
			$info =collection(model('Warninghistory')->with('enterprise')->limit($startLimit.','.$limit)->order('createTime desc')->select())->toArray();
		}else{
			// 巡检报告列表
			$info =collection(model('Warninghistory')->with('enterprise')->order('createTime desc')->select())->toArray();
		}
		$count = model('Warninghistory')->count();
		$riskDegree = [];
		$riskDegree = model('config')->where('groupName','risk_degree')->select();
		$degrees = [];
		foreach($riskDegree as $degree){
			$degrees[$degree['name']] = [
										'title' => $degree['title'],
										'color' => explode(',',$degree['value'])[2],
			];
		}
		foreach($info as &$item){
			$item['degree'] = $degrees[$item['riskDegree']]; 
		}
		return json(['code'=>1,'msg'=>'历史预警','count'=>$count,'data'=>$info]);
	}
	
	public function search($riskDegree = null,$enterpriseId = null ,$start = null,$end = null){
		$where = [] ;
		if($riskDegree){
			$where['riskDegree'] = $riskDegree;
		}
		if($enterpriseId){
			$where['enterpriseId'] = $enterpriseId;
		}
		if($start&&$end){
			$where['createTime'] = ['between time',[$start,$end]];
		}
		//分页参数
		if(($limit = input('limit')) && ($page = input('page'))){
			$startLimit =  ($page-1)*$limit;
			// 巡检报告列表
			$info =collection(model('Warninghistory')->with('enterprise')->where($where)->limit($startLimit.','.$limit)->select())->toArray();
		}else{
			// 巡检报告列表
			$info =collection(model('Warninghistory')->with('enterprise')->where($where)->select())->toArray();
		}
		//总数
		$count = model('Warninghistory')->with('enterprise')->where($where)->count('id');
		$riskDegree = [];
		$riskDegree = model('config')->where('groupName','risk_degree')->select();
		$degrees = [];
		foreach($riskDegree as $degree){
			$degrees[$degree['name']] = [
					'title' => $degree['title'],
					'color' => explode(',',$degree['value'])[2],
			];
		}
		foreach($info as &$item){
			$item['degree'] = $degrees[$item['riskDegree']];
		}
		if ($count == 0) {
			return json(['code'=>1,'msg'=>'没有找到历史数据','count'=>$count,'data'=>$info]);
		}
	
		if ($count > 0){
			return json(['code'=>1,'msg'=>'巡检报告','count'=>$count,'data'=>$info]);
		}
	}
}